package com.hongxun.budget.ui.fragment.sg;

import android.app.DatePickerDialog;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.fragment.BaseFragment;
import com.hongxun.budget.utils.DateUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.budget.utils.StringUtils;
import com.hongxun.entity.LoginInfo;
import com.hongxun.greenentity.sg.CScheduleReportEntity;

import java.util.Date;

import butterknife.BindView;

/**
 * 填写记录详情
 */
public class EditRecordInfo extends BaseFragment {
    @BindView(R.id.et1)
    EditText et1;
    @BindView(R.id.et2)
    EditText et2;
    @BindView(R.id.et3)
    TextView et3;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv1)
    TextView tv1;

    private CScheduleReportEntity entity;
    private LoginInfo loginInfo;

    public EditRecordInfo() {
    }

    @Override
    public int getLayoutId() {
        return R.layout.sg_fragment_editrecordinfo;
    }

    @Override
    public void initView() {
        loginInfo = LoginInfo.getInfo(context);
        tv1.setText(String.valueOf(SPUtils.get(context, SgStrKey.dpgqtime, "")));
        if (getArguments().getBoolean("isNewAdd", false)) {
            String uuidjdjl = StringUtils.getUuid();
            SPUtils.put(context, SgStrKey.uuidjdjl, uuidjdjl);
            entity = new CScheduleReportEntity();
            entity.setGuid(uuidjdjl);
            entity.setPGuid(String.valueOf(SPUtils.get(context, SgStrKey.uuidxm, "")));
            entity.setSName(String.valueOf(SPUtils.get(context, SgStrKey.namejd, "")));
            entity.setSRDatastate("新增");
            entity.setSRDetails("");
            entity.setSRAddtime(new Date(System.currentTimeMillis()));
            entity.setSRUser(loginInfo.getUserId());
            entity.setSRUsername(loginInfo.getUsername());
            //默认设置
            et1.setText(DateUtils.LongToStringyyMMdd_(entity.getSRAddtime().getTime()) + entity.getSName());
        } else {
            entity = SgDBUtils.getCScheduleReport(String.valueOf(SPUtils.get(context, SgStrKey.uuidjdjl, "")));
            et1.setText(entity.getSRTheme());
        }
        tv2.setText(DateUtils.LongToStringyyMMdd_(entity.getSRAddtime().getTime()));
        et2.setText(entity.getSRDetails());
        et3.setText(entity.getSRUsername());

        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DateUtils.showDatePickerDialog(getActivity(), R.layout.sg_dialog_datepicker, listener);
            }
        });

    }

    DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            String month1 = "";
            String dayOfMonth1 = "";
            if (month < 10) {
                if (month == 9) {
                    month1 = "" + (month + 1);
                } else {
                    month1 = "0" + (month + 1);
                }
            } else {
                month1 = (month + 1) + "";
            }
            if (dayOfMonth < 10) {
                dayOfMonth1 = "0" + dayOfMonth;
            } else {
                dayOfMonth1 = dayOfMonth + "";
            }
            String time = year + "-" + month1 + "-" + dayOfMonth1;
            tv2.setText(time + "");
            entity.setSRAddtime(new Date(DateUtils.StringTOLongyyMMdd2(time)));
        }
    };

    public void saveReportEntity() {
        String str1 = et1.getText().toString().trim();
        String str2 = et2.getText().toString().trim();
        entity.setSRTheme(str1);
        entity.setSRDetails(str2);
        entity.setSGuid(String.valueOf(SPUtils.get(context, SgStrKey.uuidjd, "")));
        entity.setSRUpdatetime(new Date(System.currentTimeMillis()));//保存一次更新一次时间
        SgDBUtils.upDateCScheduleReportEntity(entity);
    }
}
